/*eslint-disable*/
import OugdContract from "../proto/proto";
import {
  initWebSocket,
  sendProtoData,
  setCallback,
  close,
  callback
} from "../utils/websocket";
import { EventManager } from "../utils/EventManager";

var currentUser;
var currentCourse;
var _serverVersion = "1.0.2";
var _build = "12";

var MessageKey = {};
MessageKey.error = -2;
MessageKey.errorCode = -1;

MessageKey.login = 11;
MessageKey.logout = 12;
MessageKey.enterRoom = 13;
MessageKey.exitRoom = 14;
MessageKey.liveCourseMsg = 15;
MessageKey.reconnect = 16;

MessageKey.liveCoursePlayState = 100;
MessageKey.liveCourseSignCount = 101;

console.log("ver", _serverVersion);
console.log("build", _build);

setCallback(
  open => {
    login(getCurrentUser());
  },
  ackBuffer => {
    var data = OugdContract.decode(ackBuffer);
    console.log("ws received data", data);
    // console.log(data);
    if (data.errorCode == 0) {
      switch (data.type) {
        case MessageKey.liveCourseSignCount:
          if(currentCourse && currentCourse.id == data.liveCourseSignCount.liveCourseId) {
            currentCourse.signCount = data.liveCourseSignCount.signCount;
          }
          break;
        case MessageKey.login:
          reconnect();
          break;
        default:
          break;
      }
      EventManager.Send(data.type, data);
    } else {
      EventManager.Send(MessageKey.errorCode, data.errorCode);
    }
  },
  error => {
    EventManager.Send(MessageKey.error, error);
  }
);

function getCurrentUser() {
  currentUser = JSON.parse(localStorage.getItem("currentUser"));

  return currentUser;
}

function setCurrentCourse(course) {
  currentCourse = course;
}

function getVisitor() {
  if (currentUser && currentUser.visitor && currentUser.visitor == 1) {
    return 1;
  }
  else {
    return 0;
  }
}

function getAPITime() {
  if (currentUser && currentUser.apiTime) {
    return currentUser.apiTime;
  }
  else {
    return 0;
  }
}

function login(user) {
  currentUser = user;
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    type: MessageKey.login,
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token,
    visitor: getVisitor(),
    apiTime: getAPITime()
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

function logout() {
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    type: MessageKey.logout,
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

function reconnect() {
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    type: MessageKey.reconnect,
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token,
    visitor: getVisitor(),
    apiTime: getAPITime(),
    reconnectContract: {
      liveCourseId: (currentCourse && currentCourse.id) ? currentCourse.id : ""
    }
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

function enterRoom(courseId) {
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token,
    type: MessageKey.enterRoom,
    visitor: getVisitor(),
    apiTime: getAPITime(),
    enterRoom: {
      liveCourseId: courseId
    }
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

function exitRoom(courseId) {
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token,
    type: MessageKey.exitRoom,
    exitRoom: {
      liveCourseId: courseId
    }
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

function liveCourseMsg(content) {
  var requestData = new OugdContract.create({
    id: Date.now().toString(),
    version: _serverVersion,
    userId: getCurrentUser().userId,
    token: getCurrentUser().token,
    type: MessageKey.liveCourseMsg,
    liveCourseMsg: {
      liveCourseId: currentCourse.id,
      content: content,
      nickName: currentUser.userName
    }
  });
  console.log(requestData);
  var buffer = OugdContract.encode(requestData).finish();
  sendProtoData(buffer);
}

export {
  MessageKey,
  EventManager,
  currentUser,
  currentCourse,
  setCurrentCourse,
  login,
  enterRoom,
  exitRoom,
  liveCourseMsg
};
